package ten;

import java.util.HashMap;
import java.util.Map;

public class Racer extends Thread {
    private static Map<String, String> racer = new HashMap<String, String>(){{
//        put("pool-1-thread-1", "rabbit");
//        put("pool-1-thread-2", "tortoise");
        put("Thread-1", "兔子");
        put("Thread-0", "乌龟");
    }};
    private static String winner;
    @Override
    public void run() {
        for (int i = 1; i <= 100; i++) {
            if (Thread.currentThread().getName().equals("Thread-1") && i % 10 == 0) {
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName() + "--->" + i);
            if(gameOver(i)){
                break;
            }
        }
    }

    private boolean gameOver(int step){
        if(winner != null){
            return true;
        }else{
            if(step == 100){
                winner = Thread.currentThread().getName();
                System.out.println("winner: " + racer.get(winner));
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        Racer r1 = new Racer();
        Racer r2 = new Racer();
        r1.start();
        r2.start();
    }
}
